package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsGoodsProductService goodsProductService;
    @Autowired
    DtsOrderService orderService;
    @Autowired
    DtsCategoryService categoryService;


    @RequestMapping("/chart")
    public Object chart(){
        Map<String,Object> data = new HashMap<>();
        List<CategorySellAmts> categoryShellData = categoryService.findCategorySellData();

        String[] categoryNames = new String[categoryShellData.size()];

        for (int i = 0; i < categoryShellData.size(); i++) {
            CategorySellAmts categorySellAmts = categoryShellData.get(i);

            String name = categorySellAmts.getName();
            categoryNames[i] = name;
        }

        CategorySellVo categorySellVo = new CategorySellVo();

        categorySellVo.setCategoryNames(categoryNames);

        categorySellVo.setCategorySellData(categoryShellData);

        List<DayStatis> orderStatis = orderService.getOrderAmts();

        List<String> dayDataList  = orderStatis.stream().map(orderStati->{
            return orderStati.getDayStr();
        }).collect(Collectors.toList());

        String[] dayData = dayDataList.toArray(new String[]{});

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayData);

        List<BigDecimal> orderAmtsList = orderStatis.stream().map(orderStati ->{
            return orderStati.getAmts();
        }).collect(Collectors.toList());

        orderAmtsVo.setOrderAmtData(orderAmtsList.toArray(new BigDecimal[]{}));

        List<Integer> orderCntsList = orderStatis.stream().map(orderStati ->{
            return orderStati.getCnts();
        }).collect(Collectors.toList());

        Integer[] orderCnts= orderCntsList.toArray(new Integer[]{});
        orderAmtsVo.setOrderCntData(orderCnts);

        List<DayStatis> userDayStatis = userService.findUserStatis();

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();

        userOrderCntVo.setDayData(dayData);
        userOrderCntVo.setOrderCnt(orderCnts);

        List<Integer> userCnts = userDayStatis.stream().map(dayStati -> {
            return dayStati.getCnts();
        }).collect(Collectors.toList());

        userOrderCntVo.setUserCnt(userCnts.toArray(new Integer[]{}));

        data.put("categorySell", categorySellVo);
        data.put("orderAmts", orderAmtsVo );
        data.put("userOrderCnt", userOrderCntVo );
        return ResponseUtil.ok(data);
    }


    @RequestMapping("")
    public Object init(){
        Map<String,Object> map = new HashMap<>();
        Integer goodsTotal = goodsService.getTotal();
        map.put("goodsTotal", goodsTotal);
        Integer userTotal = userService.getTotal();
       map.put("userTotal", userTotal);
        Integer productTotal = goodsProductService.getTotal();
        map.put("productTotal",productTotal );
        Integer orderTotal = orderService.getTotal();
        map.put("orderTotal", orderTotal);
        return ResponseUtil.ok(map);
    }
}
